package Search;

import Data.FriendListData;
import Data.PrivateInformationData;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

public class UserInitStage {
    private String userid;

    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";

    public UserInitStage(String userid) {
        this.userid = userid;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public PrivateInformationData getPrivateInformationData(){

        // 创建 PrivateInformationData 实例
        PrivateInformationData userPID = new PrivateInformationData();

        // 使用反射获取 PrivateInformationData 类信息
        Class<?> privateInformationDataClass = PrivateInformationData.class;

        // 获取类的所有字段
        Field[] fields = privateInformationDataClass.getDeclaredFields();

        // 遍历字段，获取字段的名称和值
        for (Field field : fields) {
            // 设置私有字段可访问
            field.setAccessible(true);

            // 获取字段名称
            String fieldName = field.getName();

            // 根据字段名称构造 SQL 查询的列名
            String columnName = fieldName.toLowerCase();

            // 使用反射获取字段的值
            Object value = null;

            try {
                // 使用反射设置字段的值
                field.set(userPID, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return userPID;
    }


    public FriendListData userInformation() throws ClassNotFoundException, SQLException, IOException {
        //查询用户个人信息
        System.out.println("我调用了吗");
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        String sql = "select * FROM userinformation where id=?;";

        PrivateInformationData userTest=getPrivateInformationData();

        PrivateInformationData userPID = new PrivateInformationData();

        PreparedStatement operation = null;
        operation = Conn.prepareStatement(sql);
        operation.setString(1,userid);
        ResultSet resultSet=operation.executeQuery();
        if(resultSet.next())
        {
            System.out.println("找到了");
            System.out.println(userid);
            userPID.setUserid(userid);
            userPID.setEmailNumber(resultSet.getString("emailNumber"));

            String path=resultSet.getString("avatar");

            System.out.println(path);
            File file=new File(path);
            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] t=new byte[(int) file.length()];
            fileInputStream.read(t);


            userPID.setAvatar(t);
            fileInputStream.close();

            //设置用户头像，返回一个字节数组存储头像，
            System.out.println("设置用户头像啦");
            userPID.setBirth(resultSet.getDate("birth"));
            userPID.setSex(resultSet.getString("sex"));
            userPID.setSaying(resultSet.getString("saying"));
            userPID.setUserNickName(resultSet.getString("userNickName"));

            FriendListData friendListData=new FriendListData(userPID,new ArrayList<PrivateInformationData>());

            SetFriendList FriendList=new SetFriendList(userPID.getUserid(),friendListData);
            friendListData.setFriendList(FriendList.getlist());
            friendListData.setMydata(userPID);

            Conn.close();
            operation.close();
            return friendListData;
        }
        else
        {
            return new FriendListData(null,null);
        }

    }
}
